{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e6ba1c9f-970a-4c12-951c-f95436f509c8",
   "metadata": {},
   "source": [
    "### 销售数据集（用于离散程度分析）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "452d7c0e-424e-4881-ac40-d93327ac93c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 设置随机种子保证可重复性\n",
    "np.random.seed(42)\n",
    "\n",
    "# 创建基本参数\n",
    "n_records = 100\n",
    "categories = ['Electronics', 'Furniture', 'Clothing']\n",
    "regions = ['North', 'South', 'East', 'West']\n",
    "sales_people = ['SP101', 'SP102', 'SP103', 'SP104', 'SP105']\n",
    "\n",
    "# 生成日期范围\n",
    "start_date = datetime(2023, 1, 1)\n",
    "dates = [start_date + timedelta(days=np.random.randint(0, 90)) for _ in range(n_records)]\n",
    "dates.sort()\n",
    "\n",
    "# 生成销售数据\n",
    "data = {\n",
    "    'OrderID': [1000 + i for i in range(1, n_records + 1)],\n",
    "    'Date': [date.strftime('%Y-%m-%d') for date in dates],\n",
    "    'ProductCategory': np.random.choice(categories, n_records, p=[0.4, 0.3, 0.3]),\n",
    "    'Region': np.random.choice(regions, n_records),\n",
    "    'SalesPersonID': np.random.choice(sales_people, n_records)\n",
    "}\n",
    "\n",
    "# 根据产品类别生成销售金额和数量\n",
    "sales_amount = []\n",
    "quantity = []\n",
    "\n",
    "for cat in data['ProductCategory']:\n",
    "    if cat == 'Electronics':\n",
    "        sales_amount.append(round(np.random.uniform(300, 1600), 2))\n",
    "        quantity.append(np.random.choice([1, 1, 1, 2], p=[0.85, 0.1, 0.03, 0.02]))\n",
    "    elif cat == 'Furniture':\n",
    "        sales_amount.append(round(np.random.uniform(400, 1200), 2))\n",
    "        quantity.append(np.random.choice([1, 1, 2], p=[0.8, 0.15, 0.05]))\n",
    "    else:  # Clothing\n",
    "        sales_amount.append(round(np.random.uniform(40, 120), 2))\n",
    "        quantity.append(np.random.choice([1, 2, 3, 4], p=[0.6, 0.25, 0.1, 0.05]))\n",
    "\n",
    "data['SalesAmount'] = sales_amount\n",
    "data['Quantity'] = quantity\n",
    "\n",
    "# 创建DataFrame\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "# 保存为CSV\n",
    "df.to_csv('sales_data_for_dispersion_analysis.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6435c7b7-1780-41e3-9c16-9ba47cc02eb2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3e284067-c57c-4def-90e5-74f40ec6b8d7",
   "metadata": {},
   "source": [
    "### 模拟的电商数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bd2a7627-1c1c-4afb-8734-fa8ee52ec7d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 设置随机种子（保证可复现）\n",
    "np.random.seed(42)\n",
    "\n",
    "# 生成1000条模拟数据\n",
    "n = 1000\n",
    "data = {\n",
    "    \"order_id\": np.arange(1001, 1001 + n),\n",
    "    \"user_id\": np.random.randint(100, 600, size=n),\n",
    "    # 右偏分布（订单金额，含极端值）\n",
    "    \"order_amount\": np.round(np.random.exponential(scale=200, size=n) + 50, 2),\n",
    "    # 左偏分布（商品评分，集中在4.5~5.0）\n",
    "    \"product_rating\": np.clip(np.random.normal(loc=4.8, scale=0.2, size=n), 1.0, 5.0),\n",
    "    # 离散低峰度（购买数量，1~5）\n",
    "    \"purchase_quantity\": np.random.randint(1, 6, size=n),\n",
    "    # 高峰度（浏览时长，多数接近0，少数极端值）\n",
    "    \"browsing_time_min\": np.random.gamma(shape=0.5, scale=1.0, size=n),\n",
    "    # 均匀分布（折扣比例0~50%）\n",
    "    \"discount_percent\": np.random.uniform(0, 50, size=n).round(0)\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "df.to_csv(\"ecommerce_dataset.csv\", index=False)  # 保存为CSV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25a9bc2f-38bc-4eb3-a854-17c8d83f3d97",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "19db50c3-eeb9-497e-9e12-b2f6fde65b17",
   "metadata": {},
   "source": [
    "### 生成超市交易数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f99ab33-896f-40a9-a938-9210b40a8252",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 设置随机种子\n",
    "np.random.seed(42)\n",
    "\n",
    "# 生成1000条数据\n",
    "n = 1000\n",
    "data = {\n",
    "    # 基础信息\n",
    "    \"transaction_id\": [\"T\" + str(10000 + i) for i in range(n)],\n",
    "    \"date\": [datetime(2023,1,1) + timedelta(days=np.random.randint(0,365)) for _ in range(n)],\n",
    "    \"time\": [f\"{np.random.randint(8,22)}:{np.random.randint(0,60):02d}\" for _ in range(n)],\n",
    "    \n",
    "    # 客户信息（设计年龄正态分布）\n",
    "    \"customer_id\": [\"C\" + str(1000 + np.random.randint(1,4001)) for _ in range(n)],\n",
    "    \"membership_level\": np.random.choice([\"普通\",\"银卡\",\"金卡\"], p=[0.6,0.3,0.1], size=n),\n",
    "    \"age\": np.clip(np.random.normal(loc=40, scale=12, size=n).astype(int), 18, 70),\n",
    "    \n",
    "    # 交易信息（右偏金额+左偏折扣）\n",
    "    \"total_amount\": np.round(np.random.exponential(scale=150, size=n) + 10, 2),\n",
    "    \"payment_method\": np.random.choice([\"现金\",\"信用卡\",\"移动支付\"], p=[0.3,0.4,0.3], size=n),\n",
    "    \"coupon_used\": np.random.choice([True, False], p=[0.3, 0.7], size=n),\n",
    "    \"discount_rate\": np.clip(np.random.beta(a=2, b=5, size=n) * 50, 0, 50),  # 修正：添加a_min和a_max\n",
    "    \n",
    "    # 商品信息（品类比例+价格右偏）\n",
    "    \"product_category\": np.random.choice([\"食品\",\"日用品\",\"电器\",\"服饰\"], p=[0.4,0.3,0.2,0.1], size=n),\n",
    "    \"product_price\": np.round(np.random.lognormal(mean=2, sigma=1, size=n) + 1, 2),\n",
    "    \"quantity\": np.random.randint(1, 21, size=n),\n",
    "    \"weight_kg\": np.clip(np.random.normal(loc=2, scale=1.5, size=n), 0.1, 10).round(1),\n",
    "    \n",
    "    # 行为信息（高峰度浏览时长）\n",
    "    \"browsing_time_min\": np.round(np.random.gamma(shape=0.5, scale=2, size=n), 1),\n",
    "    \"staff_rating\": np.clip(np.random.normal(loc=4.5, scale=0.5, size=n), 1, 5).round(1),\n",
    "    \n",
    "    # 库存与物流（右偏库存天数）\n",
    "    \"days_in_stock\": np.random.exponential(scale=30, size=n).astype(int) + 1,\n",
    "    \"supplier\": np.random.choice([\"A\",\"B\",\"C\",\"D\"], p=[0.3,0.3,0.2,0.2], size=n),\n",
    "    \"delivery_time_hours\": np.clip(np.random.lognormal(mean=2, sigma=0.5, size=n), 1, 72).astype(int),\n",
    "    \"is_express\": np.random.choice([True, False], p=[0.15, 0.85], size=n)\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "df.to_csv(\"supermarket_transactions.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0521c029-9484-4d0d-89c2-7ea5d1a9aa69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolo11",
   "language": "python",
   "name": "yolo11"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
